extends Node
class_name GUIViewManager

@export var guiRoot:Node

var viewInstanceCount := 0
var viewInstanceMap := {}

func _ready() -> void:
	pass

func _gen_new_view_instance_id() -> int:
	var t = viewInstanceCount
	viewInstanceCount += 1
	return t

func _get_view_instance(viewInstanceId:int) -> BaseGUIView:
	return viewInstanceMap[viewInstanceId]

func open_view(viewId:StringName) -> int:
	var viewInstanceId := _gen_new_view_instance_id()
	var prefab:PackedScene = GuiViewConfig.gui_dics[viewId]
	var v = prefab.instantiate() as BaseGUIView
	v.type_id=viewId
	v.viewInstanceId = viewInstanceId
	viewInstanceMap[viewInstanceId] = v
	guiRoot.add_child(v)
	v.open()
	return viewInstanceId

func close_view(viewInstanceId:int):
	var v := _get_view_instance(viewInstanceId)
	v.close()
	viewInstanceMap.erase(viewInstanceId)
	v.queue_free()
